<template>
  <div class="todo-grid-item" :class="todoItemClass">
    <!-- 卡片头部 -->
    <div class="card-header">
      <div class="header-left">
        <!-- 完成图标 -->
        <img v-if="todo.status === TodoStatus.COMPLETED" src="../../../assets/icon/finish.svg" alt="已完成" class="finish-icon"/>
        <!-- 置顶图标 -->
        <img v-if="todo.isTop" src="@/assets/icon/top.svg" alt="置顶" class="top-icon"/>
      </div>

      <el-tag v-if="showCategoryName && todo.categoryName" size="small"  effect="light">
        {{ todo.categoryName }}
      </el-tag>

      <!-- 标题 -->
      <h3 class="todo-title" :class="{ completed: todo.status === TodoStatus.COMPLETED }" @click="handleItemClick" @dblclick="handleItemDblClick">

        {{ todo.title || '' }}
      </h3>

      <div class="header-right">
        <el-dropdown trigger="click" @command="handleCommand">
          <el-button type="text" :icon="More"/>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="edit" :icon="Edit">编辑</el-dropdown-item>
              <el-dropdown-item
                  command="complete"
                  :icon="Check"
                  :disabled="todo.status === TodoStatus.COMPLETED || todo.status === TodoStatus.CANCELLED"
              >
                完成
              </el-dropdown-item>
              <el-dropdown-item command="pin" :icon="Top" divided>
                {{ todo.isTop ? '取消置顶' : '置顶' }}
              </el-dropdown-item>
              <el-dropdown-item command="favorite" :icon="Star">
                {{ todo.isFavorite ? '取消收藏' : '收藏' }}
              </el-dropdown-item>
              <el-dropdown-item command="delete" :icon="Delete" divided>删除</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <!-- 卡片内容 -->
    <div class="card-content" @click="handleItemClick" @dblclick="handleItemDblClick">
      <div v-if="todo.content" class="todo-description">
        <MarkdownRenderer :content="todo.content" theme="classic"/>
      </div>

      <!-- 标签区域 -->
      <div v-if="todo.tags && todo.tags.length > 0" class="todo-tags">
        <el-tag
            v-for="tag in parsedTags"
            :key="tag"
            size="small"
            type="info"
            effect="plain"
            class="tag-item"
        >
          {{ tag }}
        </el-tag>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import {computed} from 'vue'
import {ElMessageBox} from 'element-plus'
import {
  Edit,
  Delete,
  Check,
  Top,
  More,
  Star
} from '@element-plus/icons-vue'
import {TodoStatus, TodoPriority, type TodoItem, type TodoCategory, type UpdateTodoRequest} from '../types'
import MarkdownRenderer from '@/components/MarkdownRenderer.vue'

interface Props {
  todo: TodoItem
  category?: TodoCategory
  categories?: TodoCategory[]
  showCategoryName?: boolean
  selected?: boolean
}

interface Emits {
  (e: 'update', id: string, request: UpdateTodoRequest): void

  (e: 'delete', id: string): void

  (e: 'toggle', id: string): void

  (e: 'detail', id: string): void

  (e: 'select', id: string): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()


// 解析标签
const parsedTags = computed(() => {
  if (!props.todo.tags) return []
  try {
    return typeof props.todo.tags === 'string' ? JSON.parse(props.todo.tags) : props.todo.tags
  } catch {
    return []
  }
})

// 卡片样式类
const todoItemClass = computed(() => ({
  'completed': props.todo.status === TodoStatus.COMPLETED,
  'urgent': props.todo.priority === TodoPriority.URGENT,
  'high-priority': props.todo.priority === TodoPriority.HIGH,
  'is-selected': props.selected
}))

// 处理项目单击 - 选中
const handleItemClick = () => {
  emit('select', props.todo.id)
}

// 处理项目双击 - 跳转到详情页
const handleItemDblClick = () => {
  emit('detail', props.todo.id)
}

// 处理下拉菜单命令
const handleCommand = async (command: string) => {
  if (command === 'edit') {
    emit('update', props.todo.id, props.todo)
  } else if (command === 'complete') {
    emit('update', props.todo.id, {status: TodoStatus.COMPLETED})
  } else if (command === 'pin') {
    emit('update', props.todo.id, {isTop: !props.todo.isTop})
  } else if (command === 'favorite') {
    emit('update', props.todo.id, {isFavorite: !props.todo.isFavorite})
  } else if (command === 'delete') {
    try {
      await ElMessageBox.confirm('确认删除这个待办事项吗？', '确认删除', {
        type: 'warning'
      })
      emit('delete', props.todo.id)
    } catch {
      // 用户取消
    }
  }
}

</script>

<style scoped>
.todo-grid-item {
  background: var(--el-bg-color);
  border: 1px solid var(--el-border-color-light);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  transition: all 0.3s ease;
  cursor: pointer;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.todo-grid-item:hover {
  border-color: var(--el-color-primary);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.todo-grid-item.completed {
  opacity: 0.7;
  background: var(--el-fill-color-lighter);
}

.todo-grid-item.urgent {
  border-left: 4px solid var(--el-color-danger);
}

.todo-grid-item.high-priority {
  border-left: 4px solid var(--el-color-warning);
}

.todo-grid-item.is-selected {
  border-color: var(--el-color-primary);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.finish-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.top-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.header-right {
  opacity: 1;
  flex-shrink: 0;
}

.card-content {
  flex: 1;
  cursor: pointer;
}

.todo-title {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  line-height: 1.4;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
  text-align: center;
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;
}

.category-name-tag {
  flex-shrink: 0;
  color: #fff !important;
}

.todo-title.completed {
  text-decoration: line-through;
  color: var(--el-text-color-disabled);
}

.todo-description {
  margin: 0;
  font-size: 14px;
  color: var(--el-text-color-regular);
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.todo-description :deep(.markdown-content) {
  font-size: 14px;
  line-height: 1.5;
}

.todo-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 8px;
}

.tag-item {
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid var(--el-border-color-lighter);
  padding-top: 12px;
  margin-top: auto;
}

.footer-left {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.footer-right {
  display: flex;
  align-items: center;
}

.due-date {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--el-text-color-regular);
}

.due-date.overdue {
  color: var(--el-color-danger);
}

.priority-tag,
.status-tag,
.category-tag {
  font-size: 12px;
}

.category-tag {
  color: #fff !important;
}

@media (max-width: 768px) {
  .todo-grid-item {
    padding: 12px;
  }

  .todo-title {
    font-size: 14px;
  }

  .todo-description {
    font-size: 13px;
  }
}

/* 深色模式适配 */
.dark .todo-grid-item {
  background: #303030;
  border-color: #3a3a3a;
}

.dark .todo-grid-item:hover {
  border-color: var(--el-color-primary);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.dark .todo-grid-item.completed {
  background: #282828;
}

.dark .todo-grid-item.is-selected {
  border-color: var(--el-color-primary);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
}
</style>